package com.iqiyi.pps.epg.core.model.auth;

import com.google.common.collect.Lists;
import com.iqiyi.kiwi.utils.ConvertUtils;
import com.iqiyi.pps.epg.core.model.base.AuditableEntity;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 用户.
 * 
 * 使用JPA annotation定义ORM关系.
 * 使用Hibernate annotation定义JPA 1.0未覆盖的部分.
 * 
 */
@Entity
@Table(name = "show_auth_user")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class SysUser extends AuditableEntity implements UserDetails{

    private static final long serialVersionUID = -6019834235894602207L;
    private String loginName;               // 登录名
	private String password;                // 密码
	private String name;                    // 姓名
    private String description;             // 描述
	private String email;                   // 电子邮件
    private String phone;                   // 手机
    private Integer area;                    //地区
    private Boolean enabled = false;        // ID标识
    protected Long id;

	public void setId(Long id) {
		this.id = id;
	}
    private List<Authority> authList = Lists.newArrayList();//有序的关联对象集合
	private List<Role> roleList = Lists.newArrayList();//有序的关联对象集合
    @Override
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
		return id;
	}
	@Column(nullable = false, unique = true)
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

    public Integer getArea() {
        return area;
    }

    public void setArea(Integer area) {
        this.area = area;
    }
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    //多对多定义
    @ManyToMany(fetch = FetchType.EAGER)
    @Cascade( { org.hibernate.annotations.CascadeType.MERGE })
    //中间表定义,表名采用默认命名规则
    @JoinTable(name = "show_auth_user_authority", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "AUTH_ID") })
    //Fecth策略定义
    @Fetch(FetchMode.SUBSELECT)
    //集合按id排序.
    @OrderBy("id")
    //集合中对象id的缓存.
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public List<Authority> getAuthList() {
        return authList;
    }

    public void setAuthList(List<Authority> authList) {
        this.authList = authList;
    }

    //多对多定义
	@ManyToMany(fetch = FetchType.EAGER)

    @Cascade( { org.hibernate.annotations.CascadeType.MERGE })
	//中间表定义,表名采用默认命名规则
	@JoinTable(name = "show_auth_user_role", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	//Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	//集合按id排序.
	@OrderBy("id")
	//集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	//非持久化属性.
	@Transient
	public String getRoleNames() {
		return ConvertUtils.convertElementPropertyToString(roleList, "name", ", ");
	}

    /**
     * 用户拥有的权限名称字符串, 多个角色名称用','分隔.
     */
    //非持久化属性.
    @Transient
    public String getAuthNames() {
        return ConvertUtils.convertElementPropertyToString(authList, "name", ", ");
    }

	/**
	 * 用户拥有的角色id字符串, 多个角色id用','分隔.
	 */
	//非持久化属性.
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getRoleIds() {
		return ConvertUtils.convertElementPropertyToList(roleList, "id");
	}

    /**
     * 用户拥有的权限id字符串, 多个角色id用','分隔.
     */
    //非持久化属性.
    @Transient
    @SuppressWarnings("unchecked")
    public List<Long> getAuthIds() {
        return ConvertUtils.convertElementPropertyToList(authList, "id");
    }

    private Set<GrantedAuthority> authorities;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    @Override
    @Transient
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    @Transient
    public String getUsername() {
        return name;
    }

    @Override
    @Transient
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    @Transient
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    @Transient
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    @Transient
    public boolean isEnabled() {
        return enabled;
    }

}